package com.bbg.amyam.web.util.menu;

import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;

public class MenuItem implements java.io.Serializable {

	private String id;
	private String target;
	private String role;
	private ArrayList<MenuItem> children=new ArrayList<MenuItem>();
	private MenuItem parent=null;
	private boolean active;
	private MenuItem activeChild=null;
	private boolean visiable=true;
	private boolean defalutMenuItem;
	private String include;
	private String exclude;
	private String category="*";
	private String unitName="";
	private String domain;
	private String controllertemplate;
	private ArrayList<String> excludedFileds=new ArrayList<String>();
	private String type;
	
	public String getType() {
		return type;
	}
	public MenuItem() {
		
	}
	public MenuItem(MenuItem item) {
		this.id=item.getId();
		this.target=item.getTarget();
		this.role=item.getRole();
		this.visiable=item.isVisiable();
		this.category=item.category;
	}

	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public void setId(String value) {
		this.id=value;
	}

	public void setTarget(String value) {
		this.target=value;
	}

	public void setRole(String value) {
		this.role=value;
	}

	public String getId() {
		return id;
	}

	public String getTarget() {
		return target;
	}

	public String getTargetURL() {
		return this.unitName+"/"+this.target;
	}
	
	public String getRole() {
		return role;
	}

	public void addChild(MenuItem childMenu) {
		this.children.add(childMenu);
	}

	public ArrayList<MenuItem> getChildren() {
		return children;
	}

	public void setChildren(ArrayList<MenuItem> children) {
		this.children = children;
	}

	public boolean isAuthorized(String[] granted){
		if(StringUtils.isEmpty(this.role)){
			return true;
		}else{
			return StringUtils.indexOfAny(this.role, granted)>-1;
		}
	}
	public void setParent(MenuItem p) {
		this.parent=p;
	}
	public MenuItem getParent() {
		return parent;
	}
	public void setActive(boolean b) {
		this.active=b;
		
	}
	public boolean isActive() {
		return active;
	}
	public void setActiveChild(MenuItem activeMenu) {
		this.activeChild=activeMenu;
	}
	public String getActiveTarget(){
		if(this.activeChild==null || this.activeChild.getParent()==null){
			this.activeChild=this.children.get(0);
		}
		return this.activeChild.getId();
	}
	public void setVisiable(boolean b) {
		this.visiable=b;
		
	}
	public boolean isVisiable() {
		return visiable;
	}
	public boolean isHasChild() {
		return this.children!=null && this.children.size()>0 ;
	}
	public void setDefalutMenuItem(boolean b) {
		defalutMenuItem=b;
	}
	public boolean isDefalutMenuItem() {
		return defalutMenuItem;
	}
	public void setInclude(String value) {
		include=value;
	}
	public void setExclude(String value) {
		exclude=value;
	}
	
	public boolean isIncluded(String model){
		if(StringUtils.isEmpty(this.include)){
			return true;
		}else{
			return StringUtils.indexOfAny(this.include, model)>-1;
		}
	}
	
	
	public boolean isExcluded(String model){
		if(StringUtils.isEmpty(this.exclude)){
			return false;
		}else{
			return StringUtils.indexOfAny(this.exclude, model)>-1;
		}
	}
	public String getCategoryValue() {
		MenuItem menu = getCategoryParent();
		return menu==null?null:menu.getCategory();
	}

	
	private MenuItem getCategoryParent(){
		if(this.category!=null)
			return this;
		
		String categoryValue=this.category;
		MenuItem myParent = this;
		while(categoryValue==null && myParent!=null){
			myParent=myParent.getParent();
			categoryValue=myParent.getCategory();
		}
		
		return myParent;
	}
	
	public String getTargetParent(){
		
		String parentTargett=this.target;
		MenuItem myParent = this;
		while( myParent!=null){
			parentTargett=myParent.getTarget();
			myParent=myParent.getParent();
		}
		
		return parentTargett;
	}
	
	public void setUnitName(String currentUnitName) {
		this.unitName=currentUnitName;
	}
	public void setDomain(String value) {
		domain=value;
	}
	public void setControllertemplate(String value) {
		this.controllertemplate=value;
	}
	public String getDomain() {
		return domain;
	}
	public String getControllertemplate() {
		return controllertemplate;
	}
	public boolean isExculededDomainField(String field) {
		return this.excludedFileds.contains(field);
	}
	public void addExeculdeDomainFiled(String field){
		this.excludedFileds.add(field);
	}
	public boolean checkForView(String schoolShortName) {
		if("*".equals(this.category)){
			return true;
		}
		if("default".equals(this.category) && "default".equals(schoolShortName)){
			return true;
		}
		if("school".equals(this.category) && !"default".equals(schoolShortName)){
			return true;
		}
		return false;
	}
	public void setType(String value) {
		this.type=value;
		
	}
	
}
